const app = getApp(),url = app.globalData.url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList:[],
    isLoading: false,
    jobFairId: '',
    hasJoin: false,
    search:{
      pageNum: 1,
      pageSize: 10,
      enterprise_uid: "",
    },
    zphAddress:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    this.userInfo = wx.getStorageSync('userInfo')
    this.data.search.enterprise_uid = this.userInfo.enterpriseUid
    this.setData({
      jobFairId: options.id,
      zphAddress: options.address
    })
    this.getPositionList()
  },
  
  getPositionList(){
    let _this = this
    this.setData({ isLoading: true })
    wx.showLoading()
    wx.request({
      url: url + 'enterprise/getPositionByEnterpriseId?',
      data: this.data.search,
      method: "GET",
      success(res){
        let resData = res.data
        if(resData.code != 200 || !resData.data){
          wx.showToast({
            title: res.data.msg,
            icon:'error'
          })
          return
        }
        let dataList = (resData.data.content || []).filter(item => item.id)
        if(_this.data.search.pageNum > 1 && dataList.length === 0){
          wx.showToast({
            title: '到底了~~',
            icon:'none'
          })
          return
        }
        dataList.forEach(item => item.checked = false)
        _this.setData({
          dataList: [..._this.data.dataList, ...dataList]
        })
      },
      complete(){
        wx.hideLoading()
        _this.setData({ isLoading: false})
      }
    })
  },
  onItemChecked(e){
    let {index} = e.currentTarget.dataset
    this.data.dataList[index].checked = !this.data.dataList[index].checked
    this.setData({
      dataList:  this.data.dataList
    })
  },
  onConfirm(){
    let checkItems = this.data.dataList.filter(item => item.checked)
    if(!checkItems.length){
      wx.showToast({
        title: '请选择参会职位！',
        icon: 'none'
      })
      return
    }
    let zprs = checkItems.map(item => item.zprs).reduce((m,n) => m + n)
    let params = {
      addressDetail: this.data.zphAddress,
      enterpriseUid: this.userInfo.enterpriseUid,
      jobFairId: this.data.jobFairId,
      joinStatus: 2,
      recruitPositionNum: checkItems.length,
      zprs,
    }
    let _this = this
    wx.request({
      url: url + 'jobFair/enterpriseJoinJopFair',
      header:{ 'Content-Type': 'application/json',},
      data: params,
      method: "POST",
      success(res){
        let resData = res.data
        if (resData.code != 200) {
          wx.showToast({
            title: '申请失败',
            icon:'error'
          })
          return
        }
        _this.selectAddJFPosition(checkItems)
      }
    })
  },
  selectAddJFPosition(checkedPositions){
    let params = []
    checkedPositions.forEach(item => {
      params.push({
        enterpriseUid: this.userInfo.enterpriseUid,
        jfId: this.data.jobFairId,
        positionId: item.id
      })
    })
    
    wx.request({
      url: url + 'jobFair/selectAddJFPosition',
      header:{ 'Content-Type': 'application/json',},
      data: params,
      method: "POST",
      success(res){
        let resData = res.data
        if (resData.code != 200) {
          wx.showToast({
            title: resData.msg,
            icon:'error'
          })
          return
        }
        wx.showModal({
          title: '提示',
          content: '申请成功！',
          success() {
            wx.reLaunch({
              url: '/HomePage/jobfair/jobfair?id=2'
            })
          }
        })

        
      },
      complete(){
      }
    })
  },
  onReachBottom: function () {
    this.data.search.pageNum++
    this.setData({
      search: this.data.search
    })
    this.getPositionList()  
  },
})